package org.kabeja.dxf.entity;

import org.kabeja.dxf.DXFConstants;
import org.kabeja.dxf.helpers.Point;

/**
 * @author shutao
 * @since 2022/4/24 17:49
 */
public class DXFInsert extends DXFEntity {
    private Point insertPoint = new Point();
    private double scale_x = 1.0D;
    private double scale_y = 1.0D;
    private double scale_z = 1.0D;
    private double rotate = 0.0D;
    private int rows = 1;
    private int columns = 1;
    private double row_spacing = 0.0D;
    private double column_spacing = 0.0D;
    private String blockID = "";

    public DXFInsert() {
        this.setLineType(this.getType());
    }

    @Override
    public Bounds getBounds() {
        Bounds var1 = new Bounds();
        DXFBlock var2 = this.doc.getDXFBlock(this.getBlockID());
        Bounds var3 = var2.getBounds();
        if (!var3.isValid()) {
            var1.setValid(false);
            return var1;
        } else {
            Point var4 = var2.getReferencePoint();
            var1.setMaximumX((var3.getMaximumX() - var4.getX()) * this.scale_x);
            var1.setMinimumX((var3.getMinimumX() - var4.getX()) * this.scale_x);
            var1.setMaximumY((var3.getMaximumY() - var4.getY()) * this.scale_y);
            var1.setMinimumY((var3.getMinimumY() - var4.getY()) * this.scale_y);
            if (this.rotate != 0.0D) {
                Point var5 = this.rotatePoint(var1.getMaximumX(), var1.getMaximumY());
                Point var6 = this.rotatePoint(var1.getMaximumX(), var1.getMinimumY());
                Point var7 = this.rotatePoint(var1.getMinimumX(), var1.getMaximumY());
                Point var8 = this.rotatePoint(var1.getMinimumX(), var1.getMinimumY());
                var1 = new Bounds();
                var1.addToBounds(var5);
                var1.addToBounds(var6);
                var1.addToBounds(var7);
                var1.addToBounds(var8);
            }

            var1.setMaximumX(var1.getMaximumX() + this.insertPoint.getX());
            var1.setMinimumX(var1.getMinimumX() + this.insertPoint.getX());
            var1.setMaximumY(var1.getMaximumY() + this.insertPoint.getY());
            var1.setMinimumY(var1.getMinimumY() + this.insertPoint.getY());
            double var9 = (double)(this.columns - 1) * this.column_spacing;
            double var10 = (double)(this.rows - 1) * this.row_spacing;
            if (var9 >= 0.0D) {
                var1.setMinimumX(var1.getMinimumX() - var9);
            } else {
                var1.setMaximumX(var1.getMaximumX() - var9);
            }

            if (var10 >= 0.0D) {
                var1.setMinimumY(var1.getMinimumY() - var10);
            } else {
                var1.setMaximumY(var1.getMaximumY() - var10);
            }

            return var1;
        }
    }

    public String getBlockID() {
        return this.blockID;
    }

    public void setBlockID(String var1) {
        this.blockID = var1;
    }

    public double getColumnSpacing() {
        return this.column_spacing;
    }

    public void setColumnSpacing(double var1) {
        this.column_spacing = var1;
    }

    public int getColumns() {
        return this.columns;
    }

    public void setColumns(int var1) {
        this.columns = var1;
    }

    public Point getPoint() {
        return this.insertPoint;
    }

    public void setPoint(Point var1) {
        this.insertPoint = var1;
    }

    public double getRotate() {
        return this.rotate;
    }

    public void setRotate(double var1) {
        this.rotate = var1;
    }

    public double getRowSpacing() {
        return this.row_spacing;
    }

    public void setRowSpacing(double var1) {
        this.row_spacing = var1;
    }

    public int getRows() {
        return this.rows;
    }

    public void setRows(int var1) {
        this.rows = var1;
    }

    public double getScaleX() {
        return this.scale_x;
    }

    public void setScaleX(double var1) {
        this.scale_x = var1;
    }

    public double getScaleY() {
        return this.scale_y;
    }

    public void setScaleY(double var1) {
        this.scale_y = var1;
    }

    public double getScaleZ() {
        return this.scale_z;
    }

    public void setScaleZ(double var1) {
        this.scale_z = var1;
    }

    private Point rotatePoint(double var1, double var3) {
        double var5 = Math.toRadians(this.rotate);
        Point var7 = new Point();
        var7.setX(var1 * Math.cos(var5) - var3 * Math.sin(var5));
        var7.setY(var1 * Math.sin(var5) + var3 * Math.cos(var5));
        return var7;
    }

    @Override
    public String getType() {
        return DXFConstants.ENTITY_TYPE_INSERT;
    }

    @Override
    public double getLength() {
        return this.doc.getDXFBlock(this.blockID).getLength();
    }
}
